//
//  THGradientView.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THGradientView.h"

@implementation THGradientView

+ (Class)layerClass
{
    return [CAGradientLayer class];
}

+ (THGradientView *)instanceWithLeftColor:(UIColor *)leftColor rightColor:(UIColor *)rightColor
{
    if (leftColor != nil && rightColor != nil)
    {
        THGradientView * view = [self new];
        [view setColors:@[leftColor,rightColor] locations:@[@(0),@(1)]];
        [view setStartPoint:CGPointMake(0.f, 0.5f) endPoint:CGPointMake(1.0f, 0.5f)];
        return view;
    }
    return nil;
}

+ (THGradientView *)instanceWithTopColor:(UIColor *)topColor bottomColor:(UIColor *)bottomColor
{
    if (topColor != nil && bottomColor != nil)
    {
        THGradientView * view = [self new];
        [view setColors:@[topColor,bottomColor] locations:@[@(0),@(1)]];
        [view setStartPoint:CGPointMake(0.5f, 0.f) endPoint:CGPointMake(0.5f, 1.0f)];
        return view;
    }
    return nil;
}

- (void)setColors:(NSArray<UIColor *> *)colors locations:(NSArray<NSNumber *> *)locations;
{
    if (colors.count >= 2 && colors.count == locations.count)
    {
        _colors = [colors copy];
        _locations = [locations copy];
        NSMutableArray * colorList = [NSMutableArray arrayWithCapacity:colors.count];
        for (UIColor * color in _colors)
        {
            [colorList addObject:(__bridge id)color.CGColor];
        }
        [(CAGradientLayer *)self.layer setLocations:_locations];
        [(CAGradientLayer *)self.layer setColors:colorList];
    }
}

- (void)setStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint
{
    _startPoint = startPoint;
    _endPoint = endPoint;
    [(CAGradientLayer *)self.layer setStartPoint:startPoint];
    [(CAGradientLayer *)self.layer setEndPoint:endPoint];
}

@end
